﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using TeamDeployer.TeamFoundation.Build.Workflow.CustomTypes;

namespace TeamDeployer.TeamFoundation.Build.Workflow.Design
{
    internal class DeploySpecEditor : UITypeEditor
    {
        // Methods
        public override object EditValue(ITypeDescriptorContext context,
                                         IServiceProvider provider,
                                         object value)
        {
            if (provider != null)
            {
                IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(
                    typeof(IWindowsFormsEditorService));
                if (service == null)
                {
                    return value;
                }
                using (DialogDeploySpec dialog = new DialogDeploySpec())
                {
                    dialog.Spec = value as DeploySpec;
                    if (service.ShowDialog(dialog) == DialogResult.OK)
                    {
                        value = dialog.Spec.Clone();
                    }
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}